#!/bin/bash
# chkconfig: 2345 10 90
es_path="/data/elk/elasticsearch-7.15.1"
cd ${es_path}

start() {
    sudo -u elkuser ./bin/elasticsearch --daemon &
    sleep 5
    if [[ -n $(ps aux|grep java|grep elasticsearch|grep conf|grep -v grep|awk '{print $2}') ]];then
        echo -e "start elasticsearch success"
    fi
}

stop() {
    es_pid=$(ps aux|grep java|grep elasticsearch|grep conf|grep -v grep|awk '{print $2}')
    kill -9 ${es_pid}
    sleep 2
    echo "stop elasticsearch success"
}

case $1 in
    start)
        start;;
    stop)
        stop;;
    restart)
        stop
        start;;
    *)
        echo "Usage: start|stop|restart"
esac
